package com.Iconcat.system.controller;

import com.Iconcat.system.entity.CatNews;
import com.Iconcat.system.response.Result;
import com.Iconcat.system.service.CatUserService;
import com.Iconcat.system.service.impl.CatNewsServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;

@RestController
@RequestMapping("/CatNews")
@CrossOrigin
@Api(value = "新闻模块",tags = "新闻接口")
public class CatNewsController {
    @Resource
    private CatNewsServiceImpl catNewsService;
    @GetMapping("/list")
    public Result UserAll(@RequestParam(required = true,defaultValue = "1") Integer current,
                          @RequestParam(required = true,defaultValue = "6")Integer size

                          ){
        Page<CatNews> page=new Page<>(current,size);
        LambdaQueryWrapper<CatNews> queryWrapper=new LambdaQueryWrapper<>();
        Page<CatNews> newsPage=catNewsService.page(page,queryWrapper);
        return Result.ok().data("news",newsPage);
    }
        @GetMapping("/{id}")
    public Result queryByKey(@PathVariable("id") Integer news_id){
            CatNews  id=catNewsService.getById(news_id);
        return Result.ok().data("news", id);
    }
    @PostMapping("/updateById")
    public Result updateById(@RequestBody CatNews catNews)throws Exception{
        Boolean admin=catNewsService.updateById(catNews);
        if (admin){
            return Result.ok().message("修改成功");
        }
        else {
            return  Result.error().message("修改失败");
        }
    }
    @PostMapping("/addNews")
    public Result addNews(@RequestBody CatNews catNews){
        try {
            catNewsService.save(catNews);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            return Result.error();
        }
    }
    @PostMapping("/delete/id")
    public Result deleteById(@RequestParam("news_id") Integer news_id){
        Boolean  admin=catNewsService.removeById(news_id);
        if (admin){
            return Result.ok().message("删除成功");
        }
        else {
            return  Result.error().message("删除失败");
        }
    }
    @PostMapping("/deleteIds")
    public Result deleteByIds(@RequestParam("ids") Collection ids){
       Boolean admin=catNewsService.removeByIds(ids);
        System.out.println(admin);
        if (admin==false){
            return Result.error().message("删除失败或数据不存在");
        }
        return Result.ok().message("删除成功");
    }
}
